package cp.test;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;

public class TCPStreamWorker implements Runnable {

    private volatile boolean running = false;
    private volatile boolean mainloop = true;
    private volatile int port;
    byte tem = 0;
    private volatile Thread tp;
    private Socket client;
    private ServerSocket serverSocket;
    private byte value1 = 0;

    TCPStreamWorker(int port) {
        this.port = port;
        this.tp = new Thread(this);
    }

    public void start() {
        running = true;
        mainloop = true;
        tp.start();
    }

    public void stop() {
        running = false;
        mainloop = false;
    }

    public void run() {
        DataOutputStream out = null;
        byte[] data;
        while (mainloop) {
            try {
                serverSocket = new ServerSocket(port);
                System.out.println("Server Created " + port);
                client = serverSocket.accept();
                OutputStream outstream = client.getOutputStream();
                out = new DataOutputStream(outstream);
                System.out.println("Client connected on port: " + port);

                while (running) {
                    data = createArray();
                    out.write(data);
                    out.flush();
                    value1 += 50;
                }

            } catch (UnknownHostException e) {
            } catch (IOException e) {
            } finally {
                if (out != null)
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (client != null)
                    try {
                        client.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                if (serverSocket != null)
                    try {
                        serverSocket.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        }
    }

    private byte[] createArray() {

        byte[] tempdata = new byte[640 * 3 * 480];
        for (int i = 0; i < tempdata.length; i += 1) {
            tempdata[i] = value1;
            // tempdata[i+1] = value2;
            // tempdata[i+2] = value3;
            // if(i%1280 == 0){
            // value2 += 50;
            // }else if(i%640 == 0){
            // value1 += 20;
            // }
        }

        return tempdata;
    }

    private void createArrayk() {

        byte[] data = new byte[640 * 480 * 3];
        for (int i = 0; i < 640 * 480 * 3; i += 3) {

            int index = 0;
            byte value = (byte) 255;

            if (i < 400 * 640 * 3 && i >= 200 * 640) {
                index = 1;
            } else if (i < 600 * 640) {
                index = 2;
            } else {
                value = 0;
            }
            for (int k = 0; k < 3; k++) {
                if (index == k) {
                    data[i] = (value);
                }
                data[i] = (tem);
            }

        }

        tem += 10;
    }
}
